<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bill extends CI_Model {
  
  var $table_name = 'bills';

  function addBill($user_id, $product_id, $date, $cost, $status,$count) {
      
    $data = array('user_id'=>$user_id, 'product_id'=>$product_id, 'status'=>$status, 'date'=>$date, 'cost'=>$cost,
    'count'=>$count);
    $this->db->insert($this->table_name, $data);
    $this->db->where($data);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }

  function editBillStatus($id, $status) {
        
    $data = array(
      'status'=>$status
    );
    $this->db->where('id',$id);
    $this->db->set($data);
		$this->db->update($this->table_name);
  }

  function editBillCount($id, $count) {
        
    $data = array(
      'count'=>$count
    );
    $this->db->where('id',$id);
    $this->db->set($data);
    $this->db->update($this->table_name);
  }

  function editBillCost($id, $amount) {
    $data = array('cost'=>$amount);
    $this->db->where('id',$id);
    $this->db->set($data);
    $this->db->update($this->table_name);

  }

  function deleteUserBill($id, $user_id){
    $this->db->where('id', $id);
    $res = $this->db->get($this->table_name)->result();
    
    if($res[0]->user_id == $user_id);
      $this->db->where('id', $id);
      $this->db->set(array('status' => 'deleted'));
      $this->db->update($this->table_name);
  }

  function deleteBill($id){
    $this->db->delete($this->table_name, array('id' => $id));
  }


  function getBillById($id){
    $this->db->where('id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }

  function getBillByUserId($id){
    $this->db->where('user_id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }
}